var app = getApp();
Page(Object.assign({},{
    data:{
        curPage : "/zofui_sales/cart/cart",
        datalist : [],
        Time : app.globalData.Time,
        mylike : [],
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
		
		var shopid = options.shopid ? options.shopid : '';
        var zfid = options.zfid ? options.zfid : '';
        
        self.setData({
            options : options,
            checkall : false,
            allmoney : 0,
            shopid : shopid,
        });
        
        app.com.auth(function(user){
            app.com.getLocation(self,function(loc){
                var postdata = {
                    op:'info',
                    lat : loc.latitude,
                    lng : loc.longitude,
                    zfid:zfid,
                    shopid:shopid,
                }
                app.com.http('cart','POST',postdata,0,false,'','',function(res){
                    if (!res.data.errno && res.data) {
                        self.setData({
                            params : res.data.data.params,
                            goodlist : res.data.data.goodlist,
                            mylike : res.data.data.mylike,
                            inited : true,
                        });
                    }
                });
            })
			app.com.setBar(app,self,shopid);
        });

        wx.setNavigationBarTitle({
          title:  '购物车',
        });
    },
    onShow(){
        if( this.data.inited ){
            this.onLoad(this.data.options);
        }
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.setData({
            isdown : true,
        });
        
        this.onLoad( this.data.options );
        this.setData({
            isdown : false,
        });
        // 关闭下拉刷新
        wx.stopPullDownRefresh();

    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    countit(){
        var self = this;
        var arr = [];
        var k=0,l=0;
        for (var i = 0; i < self.data.goodlist.length; i++) {
            var item = self.data.goodlist[i].list;
            for (var j = 0; j < item.length; j++) {
                if( item[j].checked ){
                    var ina = {cid:item[j].cid,num:item[j].num*1};
                    arr.push( ina );
                    k=i;
                    l=j;
                }
            }
        }

        if( arr.length < 1 ){
            app.com.toast('还没选择');return false;
        }else if(arr.length == 1){
            var item = self.data.goodlist[k].list[l];
            wx.setStorageSync('zfssbuy',{'gid':item.id,type:0,mapid:item.ruleid ? item.ruleid : '',mapname:item.rulename ? item.rulename : '',num:item.num});

        }else{
            wx.setStorageSync('zfssbuy',{arr:arr,type:'cart'});
        }
        var tourl = '/zofui_sales/order/confirm';
        wx.navigateTo({
          url: tourl
        });
    },
    del(e){
        var self = this;
        var postdata = {
            op:'delete',
            id : e.currentTarget.dataset.id
        }
        app.com.http('cart','POST',postdata,0,true,'','',function(res){
            app.com.toast(res.data.message);
            if (!res.data.errno && res.data) {

                for (var i = 0; i < self.data.goodlist.length; i++) {
                    var item = self.data.goodlist[i].list;

                    for (var j = 0; j < item.length; j++) {
                        
                        if( item[j].cid == postdata.id ){
                            self.data.goodlist[i].list.splice(j,1);

                            if( self.data.goodlist[i].list.length <= 0 ){
                                self.data.goodlist.splice(i,1);
                            }
                            self.setData({
                                goodlist : self.data.goodlist
                            });
                            return false;
                        }
                    }
                }
            }
        });
        self.count();
    },
    checkitem(e){
        var self = this;
        var id = e.currentTarget.dataset.id;
        for (var i = 0; i < self.data.goodlist.length; i++) {
            var item = self.data.goodlist[i].list;
            for (var j = 0; j < item.length; j++) {
                if( item[j].cid == id ){
                    // 考虑到失效的商品
                    var status = item[j].fail == 1 ? false : !item[j].checked;
                    self.setData({
                        ['goodlist.['+i+']list['+j+'].checked'] : status,
                        checkall : false,
                    });
                    if(!status){
                        self.setData({
                            ['goodlist.['+i+'].isall'] : false,
                        });
                    }
                    break;
                }
            }
        }
        self.count();
    },
    checkshop(e){
        var self = this;
        var index = e.currentTarget.dataset.i;
        var goodlist = self.data.goodlist[index].list;

        var isall = true;
        if(self.data.goodlist[index].isall){
            isall = false;
        }
        for (var i = 0; i < goodlist.length; i++) {
            self.setData({
                ['goodlist.['+index+']list['+i+'].checked'] : isall,
            });
        }
        self.setData({
            ['goodlist.['+index+'].isall'] : isall,
        })
        self.count();
    },
    count(){
        var self = this;
        var num = 0;
        var money = 0;
        for (var i in self.data.goodlist) {
            var item = self.data.goodlist[i].list;
            for (var j = 0; j < item.length; j++) {
                if( !item[j].fail && item[j].checked ){
                    money += item[j].num*item[j].show_price*1;
                }
            } 
        }
        self.setData({
            allmoney : money.toFixed(2)*1,
        });
    },
    checkall(){
        var self = this;
        self.setData({
            checkall : !self.data.checkall
        });
        for (var i = 0; i < self.data.goodlist.length; i++) {
            var item = self.data.goodlist[i].list;
            for (var j = 0; j < item.length; j++) {
                if( !item[j].fail ){
                    self.setData({
                        ['goodlist['+i+']list['+j+'].checked'] : self.data.checkall
                    });
                }
            }
        }
        self.count();
    },
    miusVal(e) {
        var self = this;
        var id = e.currentTarget.dataset.id;
        var type = e.currentTarget.dataset.type;
        var num = 1;
        for (var i = 0; i < self.data.goodlist.length; i++) {
            var item = self.data.goodlist[i].list;
            for (var j = 0; j < item.length; j++) {
                if( item[j].cid == id ){
                    
                    if( type == 1 ){
                        num = item[j].num*1 - 1;
                        if( item[j].num <= 1 ){
                            num = 1;
                        }
                    }else if(type == 2){
                        num = item[j].num*1 + 1;
                        if( item[j].num >= item[j].stock ){
                            num = item[j].stock;
                        }
                    }else if(type == 3){
                        num = e.detail.value;
                        if( num >= item[j].stock ){
                            num = item[j].stock;
                        }
                    }

                    self.setData({
                        ['goodlist['+i+']list['+j+'].num'] : num*1
                    });

                    var postdata = {
                        op:'addnum',
                        cid : id,
                        num : num,
                    }
                    app.com.http('cart','POST',postdata,0,false,'','',function(res){});
                    break;
                }
            }
        }
        self.count();
    },


}))